package com.frxuexi.park2210.repair

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2210.R
import com.frxuexi.park2210.databinding.ActivityRepairAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/RepairAddActivity")
@AndroidEntryPoint
class RepairAddActivity : BaseActivity<ActivityRepairAddBinding,RepairViewModel>() {
    override fun initData() {
        lifecycleScope.launch {
            vm.addRepairSuc.collect{
                if(it.isNotEmpty()){
                    ToastUtils.showLong(it)
                    finish()
                }
            }
        }
    }

    override fun initView() {
        binding.headerRepairadd.headerBack.setOnClickListener {
            finish()
        }
        binding.headerRepairadd.headerTitle.setText("添加维修")

        binding.btnAddrepair.setOnClickListener {
            val map = mutableMapOf<String, Any>()
            map["repairData"]=binding.etRetitle.text.toString()
            map["repairDescribe"]=binding.etRecontent.text.toString()
            map["userId"]=SPUtils.getInstance().getInt(Const.UID)
            vm.addRepair(map)
        }

        binding.etRecontent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                binding.repairTvnum.setText("${s?.length!!}/10")
                if(s?.length==10){
                    binding.repairTvnum.setTextColor(Color.RED)
                }else{
                    binding.repairTvnum.setTextColor(Color.BLACK)
                }
            }

            override fun afterTextChanged(s: Editable?) {

            }

        })
    }

}